/*
 * Copyright (c) 2021, 2022, yzfh.cn All rights reserved.
 *
 */
package com.ly.listener;

import com.ly.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @author yzfh
 * @Date 2022/02/17 星期四 10:19:20
 */
public class ContextLoaderListener implements ServletContextListener {

  @Override
  public void contextInitialized(ServletContextEvent servletContextEvent) {
    ServletContext servletContext = servletContextEvent.getServletContext();
    //读取 web.xml 中的全局参数
    String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
    ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
    //将Spring的应用上下文对象存储到ServletContext域中
    servletContext.setAttribute("app",app);
    System.out.println("Spring容器创建完毕....");
  }

  @Override
  public void contextDestroyed(ServletContextEvent servletContextEvent) {

  }
}
